package edu.guigu.appquickstart.simpleview;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;

import edu.guigu.appquickstart.R;
import edu.guigu.appquickstart.base.BaseActivity;
import edu.guigu.appquickstart.simpleview.subitem.DialogboxActivity;
import edu.guigu.appquickstart.simpleview.subitem.ImageActivity;
import edu.guigu.appquickstart.simpleview.subitem.LoginActivity;
import edu.guigu.appquickstart.utils.LogUtils;

/**
* View相关的基础入门知识
* @author Yuansir
* @since 2025/7/31 14:26
* @version V1.0
*/
public class VHomeActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vhome);
        // ActionBar处理
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(R.string.title_activity_vhome);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // 按钮监听
        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.btn_setting).setOnClickListener(this);
        findViewById(R.id.btn_image).setOnClickListener(this);
        findViewById(R.id.btn_dialog).setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_vhome, menu);

        // 到menu上拿menu_item项 ,这里是拿 search
        MenuItem menuItemSearch = menu.findItem(R.id.menu_search);
        // 到search项上拿到searchView
        SearchView searchView = (SearchView) menuItemSearch.getActionView();
        // 给searchView进行设置和文本监听
        searchView.setQueryHint("请输入关键字");
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                LogUtils.d("TAG", newText);
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home){
            finish();
        }
        if(item.getItemId() == R.id.menu_add){
            Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
        }
        if(item.getItemId() == R.id.menu_search){
            Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();
        }
        if(item.getItemId() == R.id.menu_setting){
            Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        if(v.getId() == R.id.btn_login) {
            intent.setClass(this, LoginActivity.class);
            startActivity(intent);
        }
        if(v.getId() == R.id.btn_setting) {

        }
        if(v.getId() == R.id.btn_image) {
            intent.setClass(this, ImageActivity.class);
            startActivity(intent);
        }
        if(v.getId() == R.id.btn_dialog) {
            intent.setClass(this, DialogboxActivity.class);
            startActivity(intent);
        }
    }
}